﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CBD.Conn;
using MJM.SPP.EntidadesNegocio;

namespace MJM.SPP.AccesoDatos
{
    public class PaginasAD
    {
        public int Insertar(PaginasEN objPaginasEN)
        {
            int result = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_PAGACC_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmPagina,prmTipo,prmNombre;
            try
            {
                prmPagina = cmd.CreateParameter();
                prmPagina.ParameterName = "@vPAGACC_chNOMPAG";
                prmPagina.SqlDbType = SqlDbType.VarChar;
                prmPagina.Value = objPaginasEN.PAGACC_chNOMPAG;
                cmd.Parameters.Add(prmPagina);

                prmTipo = cmd.CreateParameter();
                prmTipo.ParameterName = "@vPAGACC_inTIPPAG";
                prmTipo.SqlDbType = SqlDbType.Int;
                prmTipo.Value = objPaginasEN.PAGACC_inTIPPAG;
                cmd.Parameters.Add(prmTipo);

                prmNombre = cmd.CreateParameter();
                prmNombre.ParameterName = "@vPAGACC_chABRNOM";
                prmNombre.SqlDbType = SqlDbType.VarChar;
                prmNombre.Value = objPaginasEN.PAGACC_chABRNOM;
                cmd.Parameters.Add(prmNombre);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                result = 0;
            }

            return result;
        }

        public List<PaginasEN> Listar(int tipo,string paginas)
        {
            List<PaginasEN> lst = new List<PaginasEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_PAGACC_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            PaginasEN obj;
            SqlParameter prmTipo,prmPagina;

            try
            {
                prmTipo = cmd.CreateParameter();
                prmTipo.ParameterName = "@vPAGACC_inTIPPAG";
                prmTipo.SqlDbType = SqlDbType.Int;
                prmTipo.Value = tipo;
                cmd.Parameters.Add(prmTipo);

                prmPagina = cmd.CreateParameter();
                prmPagina.ParameterName = "@vPAGACC_chNOMPAG";
                prmPagina.SqlDbType = SqlDbType.VarChar;
                prmPagina.Value = paginas;
                cmd.Parameters.Add(prmPagina);

                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    obj = new PaginasEN();
                    obj.PAGACC_P_inID = dr.GetInt32(0);
                    obj.PAGACC_chNOMPAG = dr.GetString(1);
                    obj.Url = dr.GetString(1);
                    if (obj.Url.Length > 50)
                        obj.Url = obj.Url.Substring(0, 47) + "...";
                    obj.PAGACC_chABRNOM = dr.GetString(2);
                    obj.PAGACC_inTIPPAG = dr.GetInt32(3);
                    obj.PAGACC_inFLAELI = dr.GetInt32(4);
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            } 
            return lst;
        }

        public int Actualizar(PaginasEN objPaginasEN)
        {
            int result = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_PAGACC_Actualizar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmPagina, prmFlag, prmId, prmTipo, prmNombre;
            try
            {
                prmPagina = cmd.CreateParameter();
                prmPagina.ParameterName = "@vPAGACC_chNOMPAG";
                prmPagina.SqlDbType = SqlDbType.VarChar;
                prmPagina.Value = objPaginasEN.PAGACC_chNOMPAG;
                cmd.Parameters.Add(prmPagina);

                prmFlag = cmd.CreateParameter();
                prmFlag.ParameterName = "@vPAGACC_inFLAELI";
                prmFlag.SqlDbType = SqlDbType.Int;
                prmFlag.Value = objPaginasEN.PAGACC_inFLAELI;
                cmd.Parameters.Add(prmFlag);

                prmTipo = cmd.CreateParameter();
                prmTipo.ParameterName = "@vPAGACC_inTIPPAG";
                prmTipo.SqlDbType = SqlDbType.Int;
                prmTipo.Value = objPaginasEN.PAGACC_inTIPPAG;
                cmd.Parameters.Add(prmTipo);

                prmId = cmd.CreateParameter();
                prmId.ParameterName = "@vPAGACC_P_inID";
                prmId.SqlDbType = SqlDbType.Int;
                prmId.Value = objPaginasEN.PAGACC_P_inID;
                cmd.Parameters.Add(prmId);

                prmNombre = cmd.CreateParameter();
                prmNombre.ParameterName = "@vPAGACC_chABRNOM";
                prmNombre.SqlDbType = SqlDbType.VarChar;
                prmNombre.Value = objPaginasEN.PAGACC_chABRNOM;
                cmd.Parameters.Add(prmNombre);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                result = 0;
            }

            return result;
        }
    }
}
